package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.exceptions.BusinessException;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author W.J.Y
 */
@Service(version = "1.0.0",interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {


    @Resource
    private OrderSettingDao orderSettingDao;
    @Resource
    private OrderDao orderDao;
    @Resource
    private MemberDao memberDao;
    /**
     * 添加预约订单信息
     * @param map   预约订单信息
     * @return  添加成功的预约订单ID
     * @throws BusinessException 可能出现的业务异常
     */
    @Override
    public Integer submit(Map<String, String> map) throws BusinessException {
        //首先获取预约日期并判断该日期是否有预约设置
        String orderDateStr = map.get("orderDate");
        Date orderDate = null;
        try {
            orderDate = new SimpleDateFormat("yyyy-MM-dd").parse(orderDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new BusinessException("日期格式错误，请联系管理员！");
        }
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        if (orderSetting==null){
            throw new BusinessException(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //判断该日期是否还有可预约人数
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (number<=reservations){
            throw new BusinessException(MessageConstant.ORDER_FULL);
        }
        //判断该账号是否属于重复预约
        String idCard = map.get("idCard");
        String telephone = map.get("telephone");
        String setMealId = map.get("setmealId");
        //先判断是否有会员
        Member findMember = memberDao.findByIdCardAndPhoneNumber(idCard,telephone);
        if (findMember!=null) {
            Order findOrder = orderDao.findByOrderTwoIdAndDate(findMember.getId(), setMealId, orderDate);
            if (findOrder != null) {
                throw new BusinessException(MessageConstant.HAS_ORDERED);
            }else {
                //添加预约信息
                return getOrderId(map, orderDate, findMember);
            }
        }else {
            //无会员，注册会员并添加预约信息
            Member member = new Member();
            member.setIdCard(idCard);
            member.setName(map.get("name"));
            member.setPhoNumber(telephone);
//            member.setSex(map.get("sex"));
            int sex = Integer.parseInt(map.get("sex"));
            member.setSex(""+sex);
            memberDao.add(member);
            return getOrderId(map,orderDate,member);
        }
    }

    /**
     * 添加预约信息
     * @param map 预约信息
     * @param orderDate 预约日期
     * @param findMember    会员
     * @return  预约信息的ID
     */
    private int getOrderId(Map<String, String> map, Date orderDate, Member findMember) {
        Integer memberId = findMember.getId();
        String orderType = "微信预约";
        String orderStatus = "未到诊";
        Integer setmealId = Integer.parseInt(map.get("setmealId"));
        Order order = new Order(memberId,orderDate,orderType,orderStatus,setmealId);
        orderDao.add(order);
        //预约信息添加成功，修改预约数量
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.update(orderSetting);

        return order.getId();
    }

    /**
     * 根据预约信息ID查询预约信息
     * @param id    预约信息ID
     * @return  预约信息实例
     */
    @Override
    public Map findById(int id) {
        Map map = orderDao.findById(id);
        Date orderDate = (Date) map.get("orderDate");
        map.put("orderDate",new SimpleDateFormat("yyyy-MM-dd").format(orderDate));
        return map;
    }

    @Override
    public Result findFindOrder(Map<String, String> map) {
        orderDao.findFindOrder(map);
        return null;
    }

    @Override
    public Member mobileFindById(String id) {
        return memberDao.mobileFindById(id);
    }
}
